import { string, enum as enum_, number } from 'zod'

export const idCardValidation = string()
  .min(15, '身份证号格式不正确')
  .max(18, '身份证号格式不正确')
  .regex(
    /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
    '请输入正确的身份证号'
  )

export const phoneValidation = string()
  .min(11, '手机号格式不正确')
  .max(20, '手机号格式不正确')
  .regex(/^1[3-9]\d{9}$/, '请输入正确的手机号格式')

export const moneyValidation = string().regex(/^\d+(\.\d{1,2})?$/, '逾期本金必须是有效的金额格式')

export const stringValidation = (name = '', maxLength = 100) =>
  string().min(1, `${name}不能为空`).max(maxLength, `${name}不能超过${maxLength}个字符`)

export const enumValidation = <T extends object>(data: T, name = '') =>
  enum_(Object.keys(data), { message: `请选择有效的${name || '数据'}` })

export const defaultPageZodConfig = {
  page: number().min(1).default(1),
  limit: number().min(1).max(100).default(10),
  sortBy: string().default('createdAt'),
  sortOrder: enum_(['asc', 'desc']).default('desc')
}
